/**
 * Created by hps on 17-4-27.
 */
public class OrdArray {
    private long[] a;
    private int nElem;

    public OrdArray(int max) {
        a = new long[max];
        nElem = 0;
    }

    public int size() {
        return nElem;
    }

    public int find(long searchKey) {
        int lowerBound = 0;
        int upperBound = nElem - 1;
        int curIn;

        while (true)
        {
            curIn = (lowerBound + upperBound) / 2;
            if(a[curIn] == searchKey)
                return curIn;
            else if(lowerBound >= upperBound)
                return nElem;
            else
            {
                if(a[curIn] < searchKey)
                    lowerBound = curIn + 1;
                else
                    upperBound = curIn - 1;
            }
        }
    }

    public void insert(long value) {
        int j;
        for(j = 0; j < nElem; j++)
            if(a[j] > value)
                break;
        for (int i = nElem; i > j; i--) {
            a[i] = a[i - 1];
        }
        a[j] = value;
        nElem++;
    }

    public boolean delete(long value) {
        int j = find(value);
        if(j == nElem)
            return false;

        for (int i = j; i < nElem - 1; i++)
            a[i] = a[i + 1];
        nElem--;
        return true;
    }

    public void display() {
        for (int i = 0; i < nElem; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
}
